package com.hina.rec.endpoint.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.hina.rec.application.service.system.RoleService;
import com.hina.rec.common.model.IdReq;
import com.hina.rec.common.model.PageReq;
import com.hina.rec.common.model.Result;
import com.hina.rec.endpoint.req.system.RoleSaveReq;
import com.hina.rec.endpoint.req.system.RoleUpdateReq;
import com.hina.rec.endpoint.res.base.PageRes;
import com.hina.rec.endpoint.res.system.RoleRes;
import com.hina.rec.endpoint.res.system.SimpleRoleRes;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色控制器
 *
 * @author liaosp
 * @date 2024-01-10
 */
@Tag(name = "role", description = "角色管理相关接口")
@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
@SaIgnore
public class RoleController {

    private final RoleService roleService;

    /**
     * 创建角色
     *
     * @param req 角色信息
     * @return 操作结果
     */
    @SaCheckPermission(value = "system:role:edit")
    @PostMapping("/save")
    @Operation(summary = "创建角色", description = "创建角色")
    public Result<Boolean> save(@RequestBody @Valid RoleSaveReq req) {
        boolean result = roleService.save(req);
        return Result.success(result);
    }

    /**
     * 更新角色
     *
     * @param req 角色信息
     * @return 操作结果
     */
    @SaCheckPermission(value = "system:role:edit")
    @PostMapping("/update")
    @Operation(summary = "更新角色", description = "更新角色")
    public Result<Boolean> update(@RequestBody @Valid RoleUpdateReq req) {
        boolean result = roleService.update(req);
        return Result.success(result);
    }

    /**
     * 删除角色
     *
     * @param req id
     * @return 操作结果
     */
    @SaCheckPermission(value = "system:role:edit")
    @PostMapping("/delete")
    @Operation(summary = "删除角色", description = "根据角色ID删除角色")
    public Result<Boolean> delete(@RequestBody @Valid IdReq req) {
        boolean result = roleService.delete(req.getId());
        return Result.success(result);
    }

    /**
     * 分页查询角色
     *
     * @param req 分页参数
     * @return 角色分页列表
     */
    @SaCheckPermission(value = "system:role:query")
    @PostMapping("/page")
    @Operation(summary = "分页查询角色", description = "分页查询角色列表")
    public Result<PageRes<RoleRes>> page(@RequestBody @Valid PageReq req) {
        return Result.success(roleService.page(req));
    }

    /**
     * 获取角色精简信息列表
     *
     * @return 角色精简信息列表
     */
    @SaCheckPermission(value = "system:role:query")
    @PostMapping("/simple_list")
    @Operation(summary = "获取角色精简信息列表", description = "获取角色精简信息列表(只包含被开启的角色，主要用于前端的下拉选项)")
    public Result<List<SimpleRoleRes>> simpleList() {
        return Result.success(roleService.simpleList());
    }

    /**
     * 角色详情
     *
     * @param req 请求参数
     * @return 角色详情
     */
    @SaCheckPermission(value = "system:role:query")
    @PostMapping("/detail")
    @Operation(summary = "获取角色详情", description = "根据角色ID获取角色详情")
    public Result<RoleRes> detail(@RequestBody @Valid IdReq req) {
        return Result.success(roleService.detail(req));
    }

    /**
     * 获得角色拥有的菜单编号列表
     *
     * @param req 角色ID
     * @return 菜单ID列表
     */
    @SaCheckPermission(value = "system:role:query")
    @PostMapping("/menus")
    @Operation(summary = "获得角色拥有的菜单编号列表", description = "根据角色ID获取其拥有的菜单编号列表")
    public Result<List<Integer>> menus(@RequestBody @Valid IdReq req) {
        return Result.success(roleService.menus(req));
    }


    /**
     * 查询所有角色信息
     *
     * @return 角色列表
     */
    @SaCheckPermission(value = "system:role:query")
    @PostMapping("/list")
    @Operation(summary = "查询所有角色信息", description = "查询所有未删除的角色信息列表")
    public Result<List<RoleRes>> list() {
        return Result.success(roleService.list());
    }
}